/**
 *   Copyright (C) 2012  Development Solutions (DS) Software
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provided with
 *   this file.
 *      1. Preserve the original authors’ name.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 * 
 */
package org.dss.gil.view;

import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.dss.gil.util.Globals;
import org.dss.gil.util.FileManager;


/**
 *  About window
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.0
 */
public class About extends javax.swing.JFrame {

    private static Logger logger = Logger.getLogger(About.class.getName());
    private static About instance;
    private int width,width2;
    private FileManager fm;
    
    public static About getInstance(){
        if(instance==null){
            instance = new About();
        }
        return instance;
    }
    
    /**
     * 1.Load the swing components
     * 2.Set the window in the middle of the screen
     * 3.Set always on top (afloat)
     * 4.Set the size (Only show the About Panel first)
     * 5.Create FileManager Object
     * 6.Read the License File
     * 7.Set the license into TextArea
     */
    private About() {
        initComponents();
        setLocationRelativeTo(null);
        setAlwaysOnTop(true);
        setIconImage(Globals.logoImg);
        setLabels();
        width =  getWidth();
        width2 = jPanel1.getWidth();
        setSize(width2, getHeight());
    }
    
    public void setLabels() {
        try {
            labelApp.setText(Globals.APP_NAME+Globals.APP_VERSION);
            labelAuthorMail.setText(Globals.AUTHOR_MAIL);
            labelAuthorName.setText(Globals.AUTHOR_NAME);
            labelCorpMail.setText(Globals.CORP_MAIL);
            labelAuthor.setText(Globals.language.getMessage("label.about.auth"));
            textAuthors.setText(Globals.language.getMessage("label.about.coAuth")+Globals.CO_AUTHORS);
            
            setTitle(Globals.language.getMessage("title.about"));
            fm = FileManager.getInstance();
            String text="";
            text+= fm.readTextFile(Globals.LICENSE_PATH);
            textLicence.setText( text );
        } catch (FileNotFoundException ex) {
            logger.error(ex.getMessage());
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        labelAuthor = new javax.swing.JLabel();
        buttonOpen = new javax.swing.JButton();
        labelCorpMail = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textAuthors = new javax.swing.JTextArea();
        labelAuthorMail = new javax.swing.JLabel();
        labelAuthorName = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        labelApp = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        textLicence = new javax.swing.JTextArea();
        buttonClose = new javax.swing.JButton();

        setTitle("About");
        setResizable(false);

        labelAuthor.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        labelAuthor.setText("Author:");

        buttonOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/agt_forward.png"))); // NOI18N
        buttonOpen.setToolTipText("");
        buttonOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOpenActionPerformed(evt);
            }
        });

        labelCorpMail.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        labelCorpMail.setText("ds.developSolutions@gmail.com");

        textAuthors.setColumns(20);
        textAuthors.setRows(1);
        textAuthors.setText("Co-authors:");
        textAuthors.setOpaque(false);
        jScrollPane1.setViewportView(textAuthors);

        labelAuthorMail.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        labelAuthorMail.setText("lobho.gil@gmail.com");

        labelAuthorName.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        labelAuthorName.setText("Francisco Fernando Gil Villalobos");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/DSSoftware.png"))); // NOI18N

        labelApp.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        labelApp.setText("SimBlock 1.0");

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 18));

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(2, 2, 2)
                        .add(jLabel3)
                        .add(18, 18, 18)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(labelCorpMail)
                            .add(labelApp)
                            .add(labelAuthor)
                            .add(labelAuthorName)
                            .add(labelAuthorMail)))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(15, 15, 15)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(buttonOpen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 257, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 252, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel3)
                        .add(63, 63, 63))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(8, 8, 8)
                        .add(labelCorpMail)
                        .add(8, 8, 8)
                        .add(labelApp)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 9, Short.MAX_VALUE)
                        .add(labelAuthor)
                        .add(7, 7, 7)
                        .add(labelAuthorName)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(labelAuthorMail)
                        .add(18, 18, 18)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(buttonOpen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(19, 19, 19))
        );

        textLicence.setColumns(20);
        textLicence.setRows(5);
        jScrollPane2.setViewportView(textLicence);

        buttonClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/close_24.png"))); // NOI18N
        buttonClose.setToolTipText("");
        buttonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCloseActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, buttonClose, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(buttonClose)
                .add(18, 18, 18))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
        setSize(width2, getHeight());
    }//GEN-LAST:event_buttonCloseActionPerformed

    private void buttonOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOpenActionPerformed
        setSize(width , getHeight());
    }//GEN-LAST:event_buttonOpenActionPerformed
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClose;
    private javax.swing.JButton buttonOpen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelApp;
    private javax.swing.JLabel labelAuthor;
    private javax.swing.JLabel labelAuthorMail;
    private javax.swing.JLabel labelAuthorName;
    private javax.swing.JLabel labelCorpMail;
    private javax.swing.JTextArea textAuthors;
    private javax.swing.JTextArea textLicence;
    // End of variables declaration//GEN-END:variables

}
